package tt.dz.egobus.servicemanager.controller.businessserviceinfo;


import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tt.dz.egobus.api.ResponseBack;
import tt.dz.egobus.api.managerservice.manager.UserRequestTypeInfo;
import tt.dz.egobus.bean.BusinessStatus;
import tt.dz.egobus.bean.CriteriaRestrictionsBuilder;
import tt.dz.egobus.servicemanager.base.controller.EntityController;
import tt.dz.egobus.servicemanager.base.service.ExtendBaseService;
import tt.dz.egobus.servicemanager.entities.BusinessServiceInfo;
import tt.dz.egobus.servicemanager.service.BusinessServiceInfoService;
import tt.dz.egobus.servicemanager.service.GrabManagerService;
import tt.dz.egobus.servicemanager.vo.BusinessServiceInfoViewVo;
import tt.dz.egobus.servicemanager.vo.JsonBack;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static tt.dz.egobus.servicemanager.bean.ServiceManagerCodeStatus.*;

/**
 * 业务详情管理模块
 *
 * @author Jack
 * @date 2017/8/28
 * @name
 * @since 1.0.7
 */
@RestController
@RequestMapping("/businessServiceInfo")
public class BusinessServiceInfoController extends EntityController<BusinessServiceInfo, BusinessServiceInfoViewVo,
        String> {

    @Autowired
    @Qualifier("businessServiceInfoServiceImpl")
    private BusinessServiceInfoService service;


    @Autowired
    @Qualifier("grabManagerServiceImpl")
    private GrabManagerService grabManagerService;

    private static final Logger LOGGER = LoggerFactory.getLogger(BusinessServiceInfoController.class);

    /**
     * 取消行程
     *
     * @author Jack
     * @date 2017/8/29
     * @name
     * @since 1.0.7
     */
    @RequestMapping("/cancel")
    public JsonBack<Boolean> cancel(@RequestParam("driverId") String driverId, @RequestParam("businessId") String
            businessId) {
        if (StringUtils.isBlank(driverId)) {
            JsonBack.error(PARAM_ILLEGAL_ERROR);
        }
        if (StringUtils.isBlank(businessId)) {
            JsonBack.error(PARAM_ILLEGAL_ERROR);
        }
        ResponseBack<Boolean> result = grabManagerService.driverCancelBusiness(driverId, businessId);
        if (result != null) {
            return new JsonBack<Boolean>(result.getCode(), result.getErrorMsg(), result.getResult());
        }
        return JsonBack.error(SYSTEM_CODE_ERROR);
    }


    /**
     * 查询用于请求类型
     *
     * @author Jack
     * @date 17-8-29
     * @since 1.0.7
     */
    @RequestMapping("/getUserRequestType")
    public JsonBack<List<Map<String, Object>>> getUserRequestType() {
        try {
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            for (UserRequestTypeInfo.UserRequestType c : UserRequestTypeInfo.UserRequestType.values()) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("key", c.getCode());
                map.put("msg", c.getMsg());
                list.add(map);
            }
            return JsonBack.success(list);
        } catch (Exception e) {
            LOGGER.error("查询用于请求类型", e);
            return JsonBack.error(SYSTEM_CODE_ERROR);
        }
    }

    /**
     * 查询订单状态异常
     *
     * @author Jack
     * @date 17-8-31
     * @since 1.0.7
     */
    @RequestMapping("/getBusinessStatus")
    public JsonBack<List<Map<String, Object>>> getBusinessStatus() {
        try {
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            for (BusinessStatus c : BusinessStatus.values()) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("key", c.getCode());
                map.put("msg", c.getMsg());
                list.add(map);
            }
            return JsonBack.success(list);
        } catch (Exception e) {
            LOGGER.error("查询订单状态异常", e);
            return JsonBack.error(SYSTEM_CODE_ERROR);
        }
    }


    /**
     * @param v 新增
     * @author Anson
     * @date 17-3-14
     * @name 新增参数
     * @see CriteriaRestrictionsBuilder
     * @since 1.0.1
     */
    @RequestMapping("/insert")
    public JsonBack<BusinessServiceInfoViewVo> insert(@ModelAttribute @Valid BusinessServiceInfoViewVo v,
                                                      BindingResult result) {
        return JsonBack.error(SERVICE_IS_NO_SUPPORT);
    }


    /**
     * @param v 更新参数
     * @author Anson
     * @date 17-3-14
     * @name 更新
     * @see CriteriaRestrictionsBuilder
     * @since 1.0.1
     */
    @RequestMapping("/update")
    public JsonBack<BusinessServiceInfoViewVo> update(@ModelAttribute @Valid BusinessServiceInfoViewVo v,
                                                      BindingResult result) {
        return JsonBack.error(SERVICE_IS_NO_SUPPORT);
    }

    /**
     * @param id 删除
     * @author Anson
     * @date 17-3-14
     * @name 删除
     * @see CriteriaRestrictionsBuilder
     * @since 1.0.1
     */
    @RequestMapping("/delete")
    public JsonBack<String> delete(@RequestParam("id") String id) {
        return JsonBack.error(SERVICE_IS_NO_SUPPORT);
    }

    @Override
    protected ExtendBaseService<BusinessServiceInfo, String> getSelfService() {
        return this.service;
    }

    @Override
    protected BusinessServiceInfoViewVo newViewVo() {
        return new BusinessServiceInfoViewVo();
    }
}
